<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Python基础语法(下) - vzvixb</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="xiaoxin" /><meta name="description" content="一 高级特效 1.切片 这种经常取指定索引范围的操作， 用循环十分繁琐，因此，Python提供了切片（Slice）操作符
" /><meta name="keywords" content="Hugo, theme, even" />






<meta name="generator" content="Hugo 0.90.1 with theme even" />


<link rel="canonical" href="https://zhouxiaoxin.gitee.io/post/python/python%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95-%E4%B8%8B/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<link href="/sass/main.min.32d4dc642fec98c34c80bebb9c784c50771712b4a8a25d9f4dd9cce3534b426e.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="Python基础语法(下)" />
<meta property="og:description" content="一 高级特效
1.切片
这种经常取指定索引范围的操作，
用循环十分繁琐，因此，Python提供了切片（Slice）操作符" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://zhouxiaoxin.gitee.io/post/python/python%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95-%E4%B8%8B/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2018-08-27T20:45:00+00:00" />
<meta property="article:modified_time" content="2018-08-27T20:45:00+00:00" />

<meta itemprop="name" content="Python基础语法(下)">
<meta itemprop="description" content="一 高级特效
1.切片
这种经常取指定索引范围的操作，
用循环十分繁琐，因此，Python提供了切片（Slice）操作符"><meta itemprop="datePublished" content="2018-08-27T20:45:00+00:00" />
<meta itemprop="dateModified" content="2018-08-27T20:45:00+00:00" />
<meta itemprop="wordCount" content="4751">
<meta itemprop="keywords" content="Python," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Python基础语法(下)"/>
<meta name="twitter:description" content="一 高级特效
1.切片
这种经常取指定索引范围的操作，
用循环十分繁琐，因此，Python提供了切片（Slice）操作符"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Even</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archs</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Cates</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">About</li>
      </a><a href="/pages/runoob/">
        <li class="mobile-menu-item">runoob</li>
      </a><a href="/pages/98wubi/">
        <li class="mobile-menu-item">98wubi</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Even</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archs</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Cates</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">About</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/pages/runoob/">runoob</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/pages/98wubi/">98wubi</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">Python基础语法(下)</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-08-27 </span>
        <div class="post-category">
            <a href="/categories/python/"> Python </a>
            </div>
          <span class="more-meta"> 约 4751 字 </span>
          <span class="more-meta"> 预计阅读 10 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#一-高级特效">一 高级特效</a>
      <ul>
        <li><a href="#1切片">1.切片</a></li>
        <li><a href="#2迭代-forin">2.迭代 for&hellip;in..</a></li>
        <li><a href="#4列表生成式">4.列表生成式</a></li>
        <li><a href="#5生成器">5.生成器</a></li>
        <li><a href="#6迭代器-iterator">6.迭代器 Iterator</a></li>
      </ul>
    </li>
    <li><a href="#二-函数式编程">二 函数式编程</a>
      <ul>
        <li><a href="#1高阶函数">1.高阶函数</a></li>
        <li><a href="#2返回函数">2.返回函数</a></li>
        <li><a href="#3匿名函数">3.匿名函数</a></li>
        <li><a href="#4-装饰器">4. 装饰器</a></li>
        <li><a href="#5-偏函数">5. 偏函数</a></li>
      </ul>
    </li>
    <li><a href="#三-模块">三 模块</a>
      <ul>
        <li><a href="#1使用模块">1.使用模块</a></li>
        <li><a href="#2-安装第三方模块">2 安装第三方模块</a></li>
        <li><a href="#3__name__--__main__含义">3<code>__name__ == '__main__'</code>含义：</a></li>
        <li><a href="#4-自定义系统包">4 自定义系统包</a></li>
      </ul>
    </li>
    <li><a href="#正则表达">正则表达</a>
      <ul>
        <li><a href="#修饰符">修饰符</a></li>
        <li><a href="#compile方法">compile方法</a></li>
        <li><a href="#1-match方法">1. match方法</a></li>
        <li><a href="#2-search方法">2. search方法</a></li>
        <li><a href="#3-findall方法">3. findall方法</a></li>
        <li><a href="#4-sub方法">4. sub方法</a></li>
        <li><a href="#5-finditer方法">5. finditer方法</a></li>
        <li><a href="#6-split方法">6 split方法</a></li>
        <li><a href="#group和groups">group和groups</a></li>
        <li><a href="#命名捕获">命名捕获</a></li>
      </ul>
    </li>
    <li><a href="#pdb调试">pdb调试</a></li>
    <li><a href="#爬虫">爬虫</a></li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="一-高级特效">一 高级特效</h2>
<h3 id="1切片">1.切片</h3>
<p>这种经常取指定索引范围的操作，
用循环十分繁琐，因此，Python提供了切片（Slice）操作符</p>
<p>获取前3个元素方法：</p>
<p><strong>1&gt; 笨方法:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Michael&#39;</span><span class="p">,</span> <span class="s1">&#39;Sarah&#39;</span><span class="p">,</span> <span class="s1">&#39;Tracy&#39;</span><span class="p">,</span> <span class="s1">&#39;Bob&#39;</span><span class="p">,</span> <span class="s1">&#39;Jack&#39;</span>
<span class="p">[</span><span class="n">L</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">L</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">L</span><span class="p">[</span><span class="mi">2</span><span class="p">]]</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>2&gt; for循环:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">r</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">n</span> <span class="o">=</span> <span class="mi">3</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">L</span><span class="p">:</span>
    <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">:</span>
        <span class="n">r</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="p">);</span>
        <span class="n">i</span><span class="o">=</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>3&gt; 切片:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">r</span> <span class="o">=</span> <span class="n">L</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="c1"># 索引从0开始，到3结束，不包括3   前闭后开</span>
<span class="n">r</span> <span class="o">=</span> <span class="n">L</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:]</span>
<span class="n">L</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">- 前10个数:L[:10]
- 后10个数:L[-10:]
- 前11到22个数:L[11:22]
- 前10个数，每两个取一个:L[:10:2]  [0,2,4,6,8]
- 所有数，每5个取一个:L[::5] [0,5,10,...]
- 原样复制一个list:L[:]
</code></pre></td></tr></table>
</div>
</div><p>tuple也是一种list也可以用切片操作，只是操作的结果仍是tuple</p>
<p><strong>4&gt; 字符串 切片操作如同list和tuple一样</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;ABCDEFG&#39;</span>
<span class="nb">str</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="o">//</span><span class="s2">&#34;ABC&#34;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2迭代-forin">2.迭代 for&hellip;in..</h3>
<p><strong>1&gt; 迭代 list或tuple:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">l</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">l</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>2&gt; 迭代 dict</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="c1"># 键：</span>
<span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span><span class="s1">&#39;c&#39;</span><span class="p">:</span><span class="mi">3</span><span class="p">}</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">d</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<span class="c1"># 值：</span>
<span class="k">for</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
<span class="c1"># 键和值:</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> : </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">))</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>3&gt; 迭代 字符串</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">for</span> <span class="n">ch</span> <span class="ow">in</span> <span class="s1">&#39;ABC&#39;</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">ch</span><span class="p">)</span><span class="o">//</span><span class="n">A</span><span class="p">,</span><span class="n">B</span><span class="p">,</span><span class="n">C</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>4&gt; collections模块的Iterable类型 判断一个对象是可迭代对象</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">form</span> <span class="n">collections</span> <span class="kn">import</span> <span class="nn">Iterable</span>
<span class="nb">isinstance</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">)</span>     <span class="c1"># True</span>
<span class="nb">isinstance</span><span class="p">(</span><span class="mi">123</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">)</span>       <span class="c1"># False</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>5&gt; enumerate函数</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">可以把一个list变成索引</span><span class="o">-</span><span class="n">元素对</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">([</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="s1">&#39;B&#39;</span><span class="p">,</span> <span class="s1">&#39;C&#39;</span><span class="p">]):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="n">v</span><span class="p">)</span>
<span class="c1"># 0 A, 1 B, 2 C</span>
<span class="c1"># 5&gt; </span>
<span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="p">[(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)]:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span>
    <span class="c1">#a 1</span>
    <span class="c1">#b 2...</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="4列表生成式">4.列表生成式</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="s1">&#39;&#39;&#39;
</span><span class="s1">1&gt; list(range(1,11)) # 1,2,3...10 
</span><span class="s1">2&gt; [x * x for x in range(1,11)] # [1,4,9,16,25,...]
</span><span class="s1">3&gt; [x * x for x in range(1.11) if x%2 == 0]  # [4,16,36,64,100]
</span><span class="s1">4&gt; [m+n for m in &#39;ABC&#39; for n in &#39;XYZ&#39;]  # [&#39;AX&#39;, &#39;AY&#39;, &#39;AZ&#39;,&#39;BX&#39;,...,&#39;CZ&#39;]
</span><span class="s1">&#39;&#39;&#39;</span>
<span class="c1"># 5&gt;</span>
<span class="kn">import</span> <span class="nn">os</span> <span class="c1"># 导入os模块，模块的概念后面讲到</span>
<span class="p">[</span><span class="n">d</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)]</span> <span class="c1"># os.listdir可以列出文件和目录</span>

<span class="c1"># 6&gt; d = {&#39;x&#39; : &#39;A&#39;, &#39;y&#39; : &#39;B&#39;, &#39;z&#39; : &#39;C&#39;}</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span>
<span class="n">使用列表生成式</span>
<span class="p">[</span><span class="n">k</span><span class="o">+</span><span class="s1">&#39;=&#39;</span><span class="o">+</span><span class="n">v</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()]</span>
<span class="c1"># [&#39;y=B&#39;, &#39;x=A&#39;, &#39;z=C&#39;]</span>

<span class="c1"># 7&gt; list中所有的字符串变成大小写</span>
<span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Hello&#39;</span><span class="p">,</span> <span class="s1">&#39;World&#39;</span><span class="p">,</span> <span class="s1">&#39;IBM&#39;</span><span class="p">,</span> <span class="s1">&#39;Apple&#39;</span><span class="p">]</span>
<span class="c1"># 转换成小写</span>
<span class="p">[</span><span class="n">s</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">L</span><span class="p">]</span>
<span class="c1"># 转换成大写</span>
<span class="p">[</span><span class="n">s</span><span class="o">.</span><span class="n">swapcase</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">L</span><span class="p">]</span>

<span class="c1"># 8&gt; 内置的isinstance函数可以判断一个变量是不是字符串</span>
<span class="nb">isinstance</span><span class="p">(</span><span class="s1">&#39;ABC&#39;</span><span class="p">,</span><span class="nb">str</span><span class="p">)</span> <span class="c1"># True</span>
<span class="n">L1</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Hello&#39;</span><span class="p">,</span> <span class="s1">&#39;World&#39;</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="s1">&#39;Apple&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">]</span>
<span class="n">L2</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">.</span><span class="n">swapcase</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">L1</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="nb">str</span><span class="p">)]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">L2</span><span class="p">)</span> <span class="c1"># [&#39;HELLO&#39;,&#39;WORLD&#39;,&#39;APPLE&#39;]</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="5生成器">5.生成器</h3>
<p>在Python中，这种一边循环一边计算的机制，称为生成器：generator。
<strong>1&gt; 创建一个generator</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">L</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="o">*</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">)]</span>
<span class="n">g</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
</code></pre></td></tr></table>
</div>
</div><p>创建L和g的区别仅在于最外层的[]和()，L是一个list，而g是一个generator。</p>
<p><strong>2&gt; next() 函数获得generator的下一个返回值</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">g</span><span class="p">:</span>
    <span class="nb">print</span>
</code></pre></td></tr></table>
</div>
</div><p>3&gt; 返回done循环完毕</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">def</span> <span class="nf">fib</span><span class="p">(</span><span class="nb">max</span><span class="p">):</span>
    <span class="n">n</span><span class="p">,</span><span class="n">a</span><span class="p">,</span><span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span>
    <span class="k">while</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="nb">max</span><span class="p">:</span>
        <span class="k">yield</span> <span class="n">b</span>
        <span class="n">a</span><span class="p">,</span><span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
        <span class="n">n</span> <span class="o">=</span> <span class="n">n</span><span class="o">+</span><span class="mi">1</span>
    <span class="k">return</span> <span class="s1">&#39;done&#39;</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="6迭代器-iterator">6.迭代器 Iterator</h3>
<blockquote>
<p>我们已经知道，可以直接作用于for循环的数据类型有以下几种：
一类是集合数据类型，如list、tuple、dict、set、str等；
一类是generator，包括生成器和带yield的generator function。
这些可以直接作用于for循环的对象统称为可迭代对象：Iterable。</p>
</blockquote>
<p><strong>可以使用isinstance()判断一个对象是否是Iterable对象:</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">form</span> <span class="n">collections</span> <span class="kn">import</span> <span class="nn">Iterable</span>
<span class="nb">isinstance</span><span class="p">([],</span><span class="n">Iterable</span><span class="p">)</span>     <span class="c1"># True</span>
<span class="nb">isinstance</span><span class="p">({},</span><span class="n">Iterable</span><span class="p">)</span>     <span class="c1"># True</span>
<span class="nb">isinstance</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span><span class="p">,</span><span class="n">Iterable</span><span class="p">)</span>  <span class="c1"># True</span>
<span class="nb">isinstance</span><span class="p">(</span><span class="s1">&#39;123&#39;</span><span class="p">,</span><span class="n">Iterable</span><span class="p">)</span>  <span class="c1"># False </span>
</code></pre></td></tr></table>
</div>
</div><p><strong>1&gt; iter()</strong></p>
<blockquote>
<p>生成器都是Iterator对象，但list、dict、str虽然是Iterable，却不是Iterator。
把list、dict、str等Iterable变成Iterator可以使用iter()函数
你可能会问，为什么list、dict、str等数据类型不是Iterator？
这是因为Python的Iterator对象表示的是一个数据流，
Iterator对象可以被next()函数调用并不断返回下一个数据，
直到没有数据时抛出StopIteration错误。可以把这个数据流看做是一个有序序列，
但我们却不能提前知道序列的长度，只能不断通过next()函数实现按需计算下一个数据，
所以Iterator的计算是惰性的，只有在需要返回下一个数据时它才会计算。
Iterator甚至可以表示一个无限大的数据流，例如全体自然数。
而使用list是永远不可能存储全体自然数的。</p>
</blockquote>
<p><strong>Python的for循环本质上就是通过不断调用next()函数实现的</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">]:</span>
    <span class="k">pass</span>
<span class="c1"># -------------- 等价于： --------------</span>
<span class="n">it</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">]</span>
<span class="k">while</span> <span class="n">true</span><span class="p">:</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="c1">#获得下一个值</span>
        <span class="n">x</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
        <span class="c1"># 遇到StopIteration就退出循环</span>
        <span class="k">break</span><span class="p">;</span>
</code></pre></td></tr></table>
</div>
</div><p>生成式
生成器 generator
迭代器 iterator
可迭代的 iterable</p>
<blockquote>
<p>迭代器是惰性的,用多少生产多少;可迭代对象是一下全部返回,不管用到用不到.</p>
</blockquote>
<h2 id="二-函数式编程">二 函数式编程</h2>
<blockquote>
<p>函数式编程的一个特点就是，允许把函数本身作为参数传入另一个函数，还允许返回一个函数！</p>
</blockquote>
<h3 id="1高阶函数">1.高阶函数</h3>
<blockquote>
<p>既然变量可以指向函数，函数的参数能接收变量，那么一个函数就可以接收另一个函数作为参数，这种函数就称之为高阶函数。</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">f</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">f</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="n">add</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="nb">abs</span><span class="p">);</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>1&gt; map()</strong></p>
<blockquote>
<p>函数接收两个参数，一个是函数，一个是Iterable，
map将传入的函数依次作用到序列的每个元素，
并把结果作为新的Iterator返回。</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span>
<span class="n">r</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">f</span><span class="p">,[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
<span class="nb">list</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="c1"># [1,4,9,16,25]</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>2&gt; reduce()</strong></p>
<blockquote>
<p>reduce把一个函数作用在一个序列[x1, x2, x3, &hellip;]
上，这个函数必须接收两个参数，reduce把结果继续和序列的下一个元素做累积计算
reduce(f,[1,2,3]) == f(f(f(1,2),3),4)</p>
</blockquote>
<p><strong>将数字字符串转换成int类型</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">reduce</span>
<span class="k">def</span> <span class="nf">str2int</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">fn</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="mi">10</span> <span class="o">+</span> <span class="n">y</span>
    <span class="k">def</span> <span class="nf">char2num</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;0&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;1&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;2&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;3&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;4&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="s1">&#39;5&#39;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="s1">&#39;6&#39;</span><span class="p">:</span> <span class="mi">6</span><span class="p">,</span> <span class="s1">&#39;7&#39;</span><span class="p">:</span> <span class="mi">7</span><span class="p">,</span> <span class="s1">&#39;8&#39;</span><span class="p">:</span> <span class="mi">8</span><span class="p">,</span> <span class="s1">&#39;9&#39;</span><span class="p">:</span> <span class="mi">9</span><span class="p">}[</span><span class="n">s</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">reduce</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="nb">map</span><span class="p">(</span><span class="n">char2num</span><span class="p">,</span> <span class="n">s</span><span class="p">))</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>3&gt; filter() 过滤</strong><br>
filter()把传入的函数依次作用于每个元素，
然后根据返回值是True还是False决定保留还是丢弃该元素</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">def</span> <span class="nf">is_odd</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">n</span><span class="o">%</span><span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span>
<span class="nb">list</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="n">is_odd</span><span class="p">,[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">]))</span> <span class="c1">#[2,4]</span>

<span class="k">def</span> <span class="nf">is_empty</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">n</span> <span class="ow">and</span> <span class="n">n</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>4&gt; sorted() 排序算法</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="nb">sorted</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span><span class="n">key</span><span class="o">=</span><span class="nb">abs</span><span class="p">)</span> <span class="c1"># [1,-2,3,-4,5]</span>
<span class="nb">sorted</span><span class="p">([</span><span class="s1">&#39;bob&#39;</span><span class="p">,</span> <span class="s1">&#39;about&#39;</span><span class="p">,</span> <span class="s1">&#39;Zoo&#39;</span><span class="p">,</span> <span class="s1">&#39;Credit&#39;</span><span class="p">],</span> <span class="n">key</span><span class="o">=</span><span class="nb">str</span><span class="o">.</span><span class="n">lower</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="c1"># [&#39;Zoo&#39;, &#39;Credit&#39;, &#39;bob&#39;, &#39;about&#39;]</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2返回函数">2.返回函数</h3>
<p><strong>1&gt; 闭包</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">def</span> <span class="nf">lazy_sum</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">sum</span><span class="p">():</span>
        <span class="n">ax</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
            <span class="n">ax</span> <span class="o">=</span> <span class="n">ax</span> <span class="o">+</span> <span class="n">i</span>
        <span class="k">return</span> <span class="n">ax</span>
    <span class="k">return</span> <span class="nb">sum</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>2&gt; 闭包</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">def</span> <span class="nf">count</span><span class="p">():</span>
    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">j</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">g</span><span class="p">():</span>
            <span class="k">return</span> <span class="n">j</span><span class="o">*</span><span class="n">j</span>
        <span class="k">return</span> <span class="n">g</span>
    <span class="n">fs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">):</span>
        <span class="n">fs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="c1"># f(i)立刻被执行，因此i的当前值被传入f()</span>
    <span class="k">return</span> <span class="n">fs</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="3匿名函数">3.匿名函数</h3>
<blockquote>
<p>使用关键字lambda表示匿名函数，
匿名函数有个限制，就是只能有一个表达式，不用写return，返回值就是该表达式的结果。</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]))</span>
<span class="c1"># [1, 4, 9, 16, 25, 36, 49, 64, 81]</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="4-装饰器">4. 装饰器</h3>
<p>Python的@语法，来使用装饰器</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="c1"># 函数装饰器</span>
<span class="c1"># LEGB(local, enclosing, Globle, Build in)</span>
<span class="k">def</span> <span class="nf">deco</span><span class="p">(</span><span class="n">func</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;in deco&#39;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">in_deco</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;in in_deco&#39;</span><span class="p">)</span>
        <span class="n">func</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">in_deco</span>
<span class="nd">@deco</span> <span class="c1"># 类似于 bar = deco(bar)</span>
<span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;in bar:&#39;</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span>
<span class="n">bar</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="s1">&#39;world!&#39;</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="5-偏函数">5. 偏函数</h3>
<blockquote>
<p>当函数的参数个数太多，需要简化时，使用<strong>functools.partial</strong>可以创建一个新的函数，
这个新函数可以固定住原函数的部分参数，从而在调用时更简单。
把一个函数的某些参数给固定住（也就是设置默认值），返回一个新的函数，调用这个新函数会更简单。</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="c1"># int()函数可以把字符串转换为整数，当仅传入字符串时，int()函数默认按十进制转换：</span>
<span class="nb">int</span><span class="p">(</span><span class="s1">&#39;12345&#39;</span><span class="p">)</span> <span class="c1"># 12345</span>
<span class="c1"># 但int()函数还提供额外的base参数，默认值为10。如果传入base参数，就可以做N进制的转换：</span>
<span class="nb">int</span><span class="p">(</span><span class="s1">&#39;12345&#39;</span><span class="p">,</span> <span class="n">base</span><span class="o">=</span><span class="mi">16</span><span class="p">)</span>
<span class="c1"># 自定义一个转换2进制为10进制的函数</span>
<span class="k">def</span> <span class="nf">int2</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">base</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
    <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">base</span><span class="p">)</span>
<span class="c1"># functools.partial就是帮助我们创建一个偏函数的，不需要我们自己定义int2()</span>
<span class="kn">import</span> <span class="nn">functools</span>
<span class="n">int2</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">base</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="三-模块">三 模块</h2>
<blockquote>
<p>一个.py文件就称之为一个模块
使用模块还可以避免函数名和变量名冲突。
相同名字的函数和变量完全可以分别存在不同的模块中，
因此，我们自己在编写模块时，不必考虑名字会与其他模块冲突。
但是也要注意，尽量不要与内置函数名字冲突。</p>
</blockquote>
<blockquote>
<p>Python又引入了按目录来组织模块的方法，称为包（Package）。
请注意，每一个包目录下面都会有一个__init__.py的文件，这个文件是必须存在的
自己创建模块时要注意命名，不能和Python自带的模块名称冲突</p>
</blockquote>
<h3 id="1使用模块">1.使用模块</h3>
<p><strong>1&gt; sys模块</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding:utf-8 -*-</span>
<span class="s1">&#39;a test module&#39;</span>
<span class="n">__author_</span> <span class="p">:</span> <span class="s1">&#39;zhou&#39;</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
    <span class="n">args</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello world&#39;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello $s&#39;</span> <span class="err">$</span> <span class="p">(</span><span class="n">args</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;too many arguments&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span>
    <span class="n">test</span><span class="p">()</span>
</code></pre></td></tr></table>
</div>
</div><blockquote>
<p>当我们在命令行运行hello模块文件时，Python解释器把一个特殊变量<code>__name__</code>置为<code>__main__</code>，
而如果在其他地方导入该hello模块时，if判断将失败，因此，
这种if测试可以让一个模块通过命令行运行时执行一些额外的代码，最常见的就是运行测试</p>
</blockquote>
<p><strong>2&gt; 作用域</strong></p>
<blockquote>
<p>在一个模块中，我们可能会定义很多函数和变量，但有的函数和变量我们希望给别人使用，
有的函数和变量我们希望仅仅在模块内部使用。在Python中，是通过_前缀来实现的。</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="k">def</span> <span class="nf">_private_1</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="k">return</span> <span class="s1">&#39;Hello </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span>
<span class="k">def</span> <span class="nf">__private_2</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
    <span class="k">return</span> <span class="s1">&#39;Hello </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span>
<span class="k">def</span> <span class="nf">greeting</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">leg</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">3</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">_private_1</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">__private_2</span><span class="p">(</span><span class="n">name</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2-安装第三方模块">2 安装第三方模块</h3>
<p>在Python中，安装第三方模块，是通过包管理工具pip完成的。</p>
<h3 id="3__name__--__main__含义">3<code>__name__ == '__main__'</code>含义：</h3>
<p>如果是命令行直接运行一个py文件时候，这个文件中的<code> __name__ == '__main__'</code>
如果是另一个文件导入 这个py文件，调用另一个文件时候，这个py文件中的
<code>__name__</code> == &ldquo;这个py文件的文件名&rdquo;</p>
<h3 id="4-自定义系统包">4 自定义系统包</h3>
<ul>
<li>创建包文件夹，创建__init__.py和setup.py文件，</li>
<li>在setup.py文件中写入
from distutils.core import setup
setup(name = &lsquo;meal&rsquo;, version = &lsquo;1.0&rsquo;, py_modules = [&lsquo;test&rsquo;,&lsquo;test2&rsquo;])</li>
<li>python setup.py build, python setup.py sdist</li>
<li>最后生成disc文件夹里 压缩包 ，进入解压zxvf，</li>
<li>然后进入解压完的文件夹 运行 python setup.py install</li>
</ul>
<h2 id="正则表达">正则表达</h2>
<h3 id="修饰符">修饰符</h3>
<ul>
<li>re.I使匹配对大小写不敏感</li>
<li>re.L做本地化识别（locale-aware）匹配</li>
<li>re.M多行匹配，影响 ^ 和 $</li>
<li>re.S使 . 匹配包括换行在内的所有字符</li>
<li>re.U根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B.</li>
<li>re.X该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。</li>
</ul>
<h3 id="compile方法">compile方法</h3>
<p>compile(reg, re.S)可以将正则字符串编译成正则表达式对象，以便于在后面的匹配中复用。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">pattern</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s1">&#39;\d</span><span class="si">{2}</span><span class="s1">:\d</span><span class="si">{2}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">S</span><span class="p">)</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><p>如果使用分组<code>()</code>可以用group(1)获取单个值
使用groups()获取值组成的元祖</p>
<h3 id="1-match方法">1. match方法</h3>
<blockquote>
<p>如果使用分组()可以用group(1)获取第一个匹配<code>()</code>的值,使用groups()获取值组成的元祖,group(0)或group()返回所有匹配的字符串</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;hello world123&#39;</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;(h).*?(w)&#39;</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">S</span><span class="p">)</span>
<span class="n">res</span><span class="o">.</span><span class="n">group</span><span class="p">()</span> <span class="c1"># &#39;hello w&#39;</span>
<span class="n">res</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1"># &#39;hello w&#39;</span>
<span class="n">res</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># &#39;h&#39;</span>
<span class="n">res</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># &#39;w&#39;</span>
<span class="n">res</span><span class="o">.</span><span class="n">groups</span><span class="p">()</span> <span class="c1"># (&#39;h&#39;, &#39;w&#39;)</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2-search方法">2. search方法</h3>
<p>它在匹配时会扫描整个字符串，然后返回第一个成功匹配的结果</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">reg</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">S</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="3-findall方法">3. findall方法</h3>
<p>findall()方法会搜索整个字符串然后返回匹配正则表达式的所有内容。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">reg</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">S</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="4-sub方法">4. sub方法</h3>
<p>re.sub(pattern, repl, string, count=0)方法用来修改文本
count 模式匹配后替换的最大次数，默认 0 表示替换所有的匹配。
repl 参数是一个函数</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">&#39;\d+&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="5-finditer方法">5. finditer方法</h3>
<p>和 findall 类似，在字符串中找到正则表达式所匹配的所有子串，并把它们作为一个迭代器返回</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">re</span><span class="o">.</span><span class="n">finditer</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">string</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="6-split方法">6 split方法</h3>
<p>split 方法按照能够匹配的子串将字符串分割后返回列表</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">string</span><span class="p">[,</span> <span class="n">maxsplit</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="mi">0</span><span class="p">])</span>
</code></pre></td></tr></table>
</div>
</div><p>maxsplit: 分隔次数，maxsplit=1 分隔一次，默认为 0，不限制次数</p>
<h3 id="group和groups">group和groups</h3>
<blockquote>
<p>[注意意:只有<code>match(),serach()</code>返回值，才能用group()和groups()，findall()返回的要么<code>[(),()]</code>（正常捕获时），要么就返回<code>[,,,]</code>(无捕获组),所以<code>findall()</code>返回值并没有<code>group()</code>和<code>groups</code>方法]</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;hello world123hello world456&#39;</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s1">&#39;\w(\d+).*?(\d+)&#39;</span><span class="p">,</span><span class="nb">str</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">S</span><span class="p">)</span>
<span class="n">res</span><span class="o">.</span><span class="n">group</span><span class="p">()</span> <span class="c1"># &#39;d123hello world456&#39;</span>
<span class="n">res</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="c1"># &#39;d123hello world456&#39;</span>
<span class="n">res</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># &#39;123&#39;</span>
<span class="n">res</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># &#39;456&#39;</span>
<span class="n">res</span><span class="o">.</span><span class="n">groups</span><span class="p">()</span> <span class="c1"># (&#39;123&#39;, &#39;456&#39;)</span>
<span class="n">res</span><span class="o">.</span><span class="n">groups</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># &#39;456&#39;</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">&#39;\w(?P&lt;num1&gt;\d+).*?(?P&lt;num2&gt;\d+)&#39;</span><span class="p">,</span><span class="nb">str</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">S</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="p">)</span> <span class="c1"># (&#39;123&#39;, &#39;456&#39;)</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="命名捕获">命名捕获</h3>
<blockquote>
<p>命令组的语法是 Python 专用扩展之一： <code>(?P&lt;name&gt;...)</code>。名字很明显是组的名字。除了该组有个名字之外，命名组也同捕获组是相同的。<code>MatchObject</code> 的方法处理捕获组时接受的要么是表示组号的整数，要么是包含组名的字符串。命名组也可以是数字，<code>(?P=word)</code>获取反向引用，所以你可以通过两种方式来得到一个组的信息：</p>
</blockquote>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="n">p</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;(?P&lt;word&gt;\b\w+\b)&#39;</span><span class="p">)</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">search</span><span class="p">(</span> <span class="s1">&#39;(((( Lots of punctuation )))&#39;</span> <span class="p">)</span>
<span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s1">&#39;word&#39;</span><span class="p">)</span> <span class="c1"># &#39;Lots&#39;</span>
<span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># &#39;Lots&#39;</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="pdb调试">pdb调试</h2>
<p><code>pdb调试</code>
<strong>命令行调试</strong><br>
<code>python -m pdb xxx.py</code></p>
<blockquote>
</blockquote>
<p>h &mdash;&mdash;&mdash;&ndash;&gt; help 查看帮助
l &mdash;&mdash;&mdash;&mdash;&gt; list 显示当前的代码
n &mdash;&mdash;&mdash;&mdash;&gt; next 向下执行一行代码
c &mdash;&mdash;&mdash;&mdash;&gt; continue 继续执行代码
b &mdash;&mdash;&mdash;&mdash;&gt; break 添加断点  b 12在12行添加断电，b查看所有断点
s &mdash;&mdash;&mdash;&mdash;&gt; setp 进入到一个函数
p &mdash;&mdash;&mdash;&mdash;&gt; print 打印一个变量的值
a &mdash;&mdash;&mdash;&mdash;&gt; args 打印所有形参变量数据
q &mdash;&mdash;&mdash;&mdash;&gt; quit 退出调试
r &mdash;&mdash;&mdash;&mdash;&gt; return 快速执行到函数的最后一行</p>
<p>** 代码中使用pdb库**</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-py" data-lang="py"><span class="kn">import</span> <span class="nn">pdb</span>
<span class="n">pdb</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s2">&#34;func(11,22)&#34;</span><span class="p">)</span>
<span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span> <span class="c1"># 程序运行到这里就会停止</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="爬虫">爬虫</h2>
<p>1.明确目标 url
2.爬去
3.分析
4.储存
scrapy
新建项目
明确目标
制作爬虫
储存内容</p>
    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">xiaoxin</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2018-08-27
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/python/">Python</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/1/windows%E8%BD%AF%E4%BB%B6%E6%8E%A8%E8%8D%90/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">windows软件推荐</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/python/python%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95-%E4%B8%8A/">
            <span class="next-text nav-default">Python基础语法(上)</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:your@email.com" class="iconfont icon-email" title="email"></a>
      <a href="http://localhost:1313" class="iconfont icon-stack-overflow" title="stack-overflow"></a>
      <a href="http://localhost:1313" class="iconfont icon-twitter" title="twitter"></a>
      <a href="http://localhost:1313" class="iconfont icon-facebook" title="facebook"></a>
      <a href="http://localhost:1313" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="http://localhost:1313" class="iconfont icon-google" title="google"></a>
      <a href="http://localhost:1313" class="iconfont icon-github" title="github"></a>
      <a href="http://localhost:1313" class="iconfont icon-weibo" title="weibo"></a>
      <a href="http://localhost:1313" class="iconfont icon-zhihu" title="zhihu"></a>
      <a href="http://localhost:1313" class="iconfont icon-douban" title="douban"></a>
      <a href="http://localhost:1313" class="iconfont icon-pocket" title="pocket"></a>
      <a href="http://localhost:1313" class="iconfont icon-tumblr" title="tumblr"></a>
      <a href="http://localhost:1313" class="iconfont icon-instagram" title="instagram"></a>
      <a href="http://localhost:1313" class="iconfont icon-gitlab" title="gitlab"></a>
      <a href="http://localhost:1313" class="iconfont icon-bilibili" title="bilibili"></a>
  <a href="https://zhouxiaoxin.gitee.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2018 - 
    2022
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">even</span>
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.2517c0eb67172a0bae917de4af59b10ca2531411a009d4c0b82f5685259e5771.js"></script>








</body>
</html>
